package hotel.quartz.oversea.job.switchs;

import com.alibaba.fastjson.JSON;
import hotel.base.oversea.constant.CommonConstants;
import hotel.base.oversea.constant.CommonUrlConstants;
import hotel.base.oversea.constant.MeituanConstants;
import hotel.base.oversea.constant.enums.MessageEnums;
import hotel.base.oversea.constant.quartz.UrlSwitchsEnums;
import hotel.base.oversea.utils.DateUtil;
import hotel.base.oversea.utils.Result;
import hotel.base.oversea.utils.SendUtils;
import hotel.quartz.oversea.job.base.BaseJob;
import hotel.quartz.oversea.util.JobRestTemplateUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  触发携程接口处理很好办推送队列变化数据定时器
 * </p>
 *
 * @description: todo Switchs Pop Hhb Channel Change Goods Job
 * @author: qiuyy
 * @date: 2020-05-26 13:22
 * @version: V1.0
 */
@Slf4j
public class todoSwitchsPopHhbChannelChangeGoodsJob implements BaseJob {
    private String timestamp = "tsphccgj"+String.valueOf(System.currentTimeMillis());

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 暂时 2秒一次   触发携程接口处理缓存变化数据
     * @param jobExecutionContext
     */
    @Override
    @SneakyThrows
    public void execute(JobExecutionContext jobExecutionContext) {

        log.info("todo Switchs Pop Hhb Channel Change Goods Job {} >>>>>>>> 触发携程接口处理很好办推送队列变化数据定时器 执行时间: {}", timestamp, DateUtil.getCurrTime());
        if(0==redisTemplate.opsForList().size(MeituanConstants.REDIS_CHANGE_GOODS_DATA_MANY_HOTEL_HHB_CHANNEL)){
            log.info("todo Switchs Pop Hhb Channel Change Goods Job {} >>>>>>>> 队列size=0，定时器不触发，该次定时器跳过: {},{}",timestamp, DateUtil.getCurrTime());
        }else{
            requestDealSpider();
        }

        log.info("todo Switchs Pop Hhb Channel Change Goods Job {} >>>>>>>> 触发携程接口处理很好办推送队列变化数据定时器 结束时间: {},{}",timestamp, DateUtil.getCurrTime());

    }

    private void requestDealSpider() {
        String callbackHost = CommonUrlConstants.CALLBACK_HOST;
        String checkGetChangeGoodsByOneHotelUrl = callbackHost+ UrlSwitchsEnums.goodsPrice.SWITCH_THREAD_DEAL_REDIS_CHANGE_DATA_HHB_CHANNEL.getValue();
        log.info("todo Switchs Pop Hhb Channel Change Goods Job {} 触发携程接口处理很好办推送队列变化数据定时器 进入方法 request",timestamp);
        //发起请求
        Map params = new HashMap();
        long size = redisTemplate.opsForList().size(MeituanConstants.REDIS_CHANGE_GOODS_DATA_MANY_HOTEL_HHB_CHANNEL);

        int checkSize = 100;
        if(null != redisTemplate.opsForValue().get("todoSwitchsPopHhbChannelChangeGoodsJob_check_size_value")){
            checkSize = (Integer) redisTemplate.opsForValue().get("todoSwitchsPopHhbChannelChangeGoodsJob_check_size_value");
        }
        if(size>checkSize && null == redisTemplate.opsForValue().get("todoSwitchsPopHhbChannelChangeGoodsJob_check_size")){
            SendUtils.dingMsg("定时器处理hhb渠道产品推送队列异常！！请关注 ", DateUtil.getCurrTime()+" 队列待消费已超过"+checkSize+"，更新已堆积滞后 ", MessageEnums.genre.GOODS_STATUS_AND_PRICE.getCode(),restTemplate);
            redisTemplate.opsForValue().set("todoSwitchsPopHhbChannelChangeGoodsJob_check_size",1,30,TimeUnit.MINUTES);
        }

        log.info("todo Switchs Pop Hhb Channel Change Goods Job {} 触发携程接口处理很好办推送队列变化数据定时器  调用携程接口前，缓存队列size={}，该size不能作为依据，因为携程多线程同时在处理",timestamp,size);
        Result response = JobRestTemplateUtils.post(checkGetChangeGoodsByOneHotelUrl, JSON.toJSONString(params),restTemplate);
        long size2 = redisTemplate.opsForList().size(MeituanConstants.REDIS_CHANGE_GOODS_DATA_MANY_HOTEL_HHB_CHANNEL);
        log.info("todo Switchs Pop Hhb Channel Change Goods Job {} 触发携程接口处理很好办推送队列变化数据定时器  调用携程接口结束，缓存队列size={}，（该size不能作为依据，因为携程多线程同时在处理）",timestamp,size2);
        if(response.getCode()== CommonConstants.SUCCESS){
        }else{
            //还原
            log.error("todo Switchs Pop Hhb Channel Change Goods Job {} 触发携程接口处理很好办推送队列变化数据定时器进入方法 request 返回失败：{},{}",timestamp, JSON.toJSONString(response), JSON.toJSONString(params));
        }
    }

}
